package com.qa.controller;

import com.github.pagehelper.PageInfo;
import com.qa.common.Const;
import com.qa.common.ServerResponse;
import com.qa.pojo.*;
import com.qa.service.*;
import com.qa.vo.ArticleVo;
import com.qa.vo.CommentVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Author
 * @Date 2020/3/5 10:43
 * @Desc
 **/
@Controller
public class ArticleController {
    @Autowired
    private IArticleService articleService;

    @Autowired
    private IArticleVoService articlevoService;

    @Autowired
    private ITagService tagService;
    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private ICommentService commentService;

    @Autowired
    private ICategoryArticleService categoryArticleService;


    @PostMapping("/article/add")
    @ResponseBody
    public ServerResponse articleAdd(HttpServletRequest request, Article article) {
        return articleService.add(request, article);
    }

    @GetMapping("/my/article/")
    public String myArticle(HttpServletRequest request, Model model,
                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        User user = (User) request.getSession().getAttribute(Const.CURRENT_USER);
        PageInfo<Article> pageInfo = articleService.getByEmail(user.getEmail(), pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo.getList());
        model.addAttribute("headImg", user.getHeadImg());
        return "myArticle";
    }

    @GetMapping("/article/detail")
    public String articleDetail(@RequestParam("articleId") Integer articleId, Model model) {
        Article article = articleService.getById(articleId);
        model.addAttribute("article", article);
        if (StringUtils.isBlank(article.getTags())) {
            model.addAttribute("tags", null);
        } else {

            String[] split = article.getTags().split(",");
            List<String> list = new ArrayList<>();
            for (String s : split) {
                list.add(s);
            }
            model.addAttribute("tags", list);
        }

        List responseList = new ArrayList();

        // 查询评论
        List<Comment> comment = commentService.getByArticelId(article.getId());
        for (Comment comment1 : comment) {
            CommentVo commentVo = new CommentVo();
            List<Comment> commentList = commentService.getByCommentId(comment1.getId());
            commentVo.setComment(comment1);
            commentVo.setCommentList(commentList);
            responseList.add(commentVo);
        }
        model.addAttribute("responseList", responseList);
        return "articleDetail";
    }

    @GetMapping("/cms/article/list")
    public String articleList() {
        return "articleList";
    }

    @GetMapping("/cms/article/data")
    @ResponseBody
    public ServerResponse userList(@RequestParam(value = "articleName", defaultValue = "") String articleName,
                                   @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "limit", defaultValue = "10") Integer pageSize) {
        PageInfo<Article> pageInfo = articleService.getByArticleName(articleName, pageNum, pageSize);
        return ServerResponse.createBySuccess("查询成功！", pageInfo.getList(), pageInfo.getTotal());
    }

    @GetMapping("/cms/article/detail")
    public String cmsArticleDetail(@RequestParam("articleId") Integer articleId, Model model) {
        Article article = articleService.getById(articleId);


        List<Category> articleList =categoryService.getByStatus();
        Map tagMap = new HashMap();
        for (Category tag : articleList) {
            tagMap.put(tag.getCategoryName(), "<input  type=\"checkbox\" name=\"" + tag.getCategoryName() + "\" title=\"" + tag.getCategoryName() + "\" />");
        }

        List<CategoryArticle> categoryArticles = categoryArticleService.getByArticleId(articleId);


        for (CategoryArticle s : categoryArticles) {
            tagMap.put(s.getCaategoryName(), "<input  type=\"checkbox\" name=\"" + s.getCaategoryName() + "\"  checked=\"checked\" title=\"" + s.getCaategoryName()  + "\" />");
        }
        List list = new ArrayList();

        Collection values = tagMap.values();
        for (Object value : values) {
            list.add(value);
        }

        model.addAttribute("tagMap", list);

        model.addAttribute("article", article);
        return "articleEditPage";
    }

    @PostMapping("/cms/article/update/status")
    @ResponseBody
    public ServerResponse cmsArticleUpdateStatus(@RequestBody Article article) {
        return articleService.update(article);
    }

    @GetMapping("/article/all")
    public String allArticle(Model model, @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "limit", defaultValue = "10") Integer pageSize) {
        PageInfo<ArticleVo> pageInfo = articlevoService.getAll(pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo.getList());
        return "allArticle";
    }

    @GetMapping("/article/updateById")
    public String articleUpdateById(@RequestParam("articleId") Integer articleId, Model model, HttpServletRequest request) {
        Article article = articleService.getById(articleId);
        User user = (User) request.getSession().getAttribute(Const.CURRENT_USER);
        List<Tag> articleList = tagService.getAllByEmail(user.getEmail());
        Map tagMap = new HashMap();
        if (StringUtils.isBlank(article.getTags())) {
            for (Tag tag : articleList) {
                tagMap.put(tag.getTagName(), "<input  type=\"checkbox\" name=\"" + tag.getTagName() + "\" title=\"" + tag.getTagName() + "\" />");
            }
        } else {
            for (Tag tag : articleList) {
                tagMap.put(tag.getTagName(), "<input  type=\"checkbox\" name=\"" + tag.getTagName() + "\" title=\"" + tag.getTagName() + "\" />");
            }
            String[] split = article.getTags().split(",");
            for (String s : split) {
                tagMap.put(s, "<input  type=\"checkbox\" name=\"" + s + "\"  checked=\"checked\" title=\"" + s + "\" />");
            }

        }
        List list = new ArrayList();

        Collection values = tagMap.values();
        for (Object value : values) {
            list.add(value);
        }

        model.addAttribute("tagMap", list);
        model.addAttribute("article", article);
        return "articleDetailPage";
    }

    @GetMapping("/cms/article/delete")
    @ResponseBody
    public ServerResponse articleDelete(@RequestParam("articleId") Integer articleId) {
        return articleService.deleteById(articleId);
    }

    @GetMapping("/search/article")
    public String articleSearch(@RequestParam("articleName") String articleName, Model model) {
        List<ArticleVo> articleList = articlevoService.getAllByArticleName(articleName);
        model.addAttribute("pageInfo", articleList);
        return "allArticle";
    }

}
